<?php

require_once("RegistrationView.php");
require_once("RegistrationModel.php");
require_once("LoginController.php");
require_once("LoginView.php");
require_once("validate.php");

class RegistrationController{
	
	public function RegistrateControll(Database $db){
		
		$registrationModel = new RegistrationModel($db);
		$registrationView = new RegistrationView();
		$logincontroller = new LoginController();
		$loginview = new LoginView();
		$validate = new Validate();

		if(!$registrationModel->test($db)){
			echo "RegistrationModelTest Failed!";
		}
		
		if($registrationView->TryToRegister()){
			if(!$registrationView->PasswordCheck($registrationView->GetRegistrationPassword(), $registrationView->GetRegistrationCheckPassword())){
				return $registrationView->Messages(RegistrationView::ERROR_CHECK_FAIL) . $registrationControl->RegistrateControll($db);
			}
			if($registrationModel->RegisterNewUser(
			$registrationView->GetRegistrationUsername(), 
			$registrationView->GetRegistrationPassword()))
			{
				return $registrationView->Messages(RegistrationView::SUCCESS_NEW_USER);
			}
			else{
				$errormessage = "";
				$registrationView->ResetButtonClick();
				$registrationControl = new RegistrationController($db);
				if(!$registrationModel->ValidatePassword($registrationView->GetRegistrationPassword())){
					$errormessage .= $registrationView->Messages(RegistrationView::ERROR_SHORT_PASSWORD);
				}
				if($registrationModel->FindUserName($registrationView->GetRegistrationUsername())){
					$errormessage .= $registrationView->Messages(RegistrationView::ERROR_SAME_USERNAME);
				}
				if(!$validate->ValidatePasswordRegex($password)){
					$errormessage .= $registrationView->Messages(RegistrationView::ERROR_WRONG_CHARACTERS);
				}
				
				return $registrationView->Messages(RegistrationView::FAIL_NEW_USER) . $errormessage . $registrationControl->RegistrateControll($db);
			}
												
		}
		else{
			return $registrationView->RegistrationForm();
		}
	
	}
}
?>